#ifndef __QUANTITY_H__ 
#define __QUANTITY_H__

#include <ostream>
#include <sstream>
#include <Output.h>

//class Output;

template<class TUnit>
class Quantity
{
public:
   Quantity(int amount, const TUnit & unit):amount_m(amount), unit_m(unit){}

   bool operator==(const Quantity & num)const
   {
      return unit_m.convertToUnit(amount_m, TUnit::baseUnit()) == num.unit_m.convertToUnit(num.amount_m, TUnit::baseUnit());
   }
   bool operator!=(const Quantity & num)const
   {
      return !(*this == num);
   }

   Quantity<TUnit> operator+(const Quantity & len)const
   {
      return Quantity<TUnit> ( unit_m.convertToUnit(amount_m, TUnit::baseUnit()) + 
                   len.unit_m.convertToUnit(len.amount_m, TUnit::baseUnit()), TUnit::baseUnit() );
   } 

   Quantity<TUnit> operator-(const Quantity & len)const
   {
      return Quantity<TUnit> ( unit_m.convertToUnit(amount_m, TUnit::baseUnit()) - 
                   len.unit_m.convertToUnit(len.amount_m, TUnit::baseUnit()), TUnit::baseUnit() );
   }

   void format(Output &output)const
   {
      output.objAmountOnBaseUnit( unit_m.convertToUnit(amount_m, TUnit::baseUnit()) );
   }

   template <class T>
   friend std::ostream & operator <<(std::ostream & ostr, const Quantity<T>& quantity);

private:
   int amount_m;
   const TUnit & unit_m;
};


template <class TUnit>
std::ostream & operator << (std::ostream & ostr, const Quantity<TUnit>& quantity)
{
    if ( 0 != quantity.amount_m )
      ostr << quantity.amount_m << " " << quantity.unit_m;
    return ostr;
}


#endif

